package com.mallify.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallify.admin.service.SystemCityAsyncService;
import com.mallify.admin.service.SystemCityService;
import com.mallify.common.constants.Constants;
import com.mallify.common.model.system.SystemCity;
import com.mallify.common.request.SystemCityRequest;
import com.mallify.common.request.SystemCitySearchRequest;
import com.mallify.common.utils.RedisUtil;
import com.mallify.common.vo.SystemCityTreeVo;
import com.mallify.service.mapper.SystemCityMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * SystemCityServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Service
public class SystemCityServiceImpl extends ServiceImpl<SystemCityMapper, SystemCity> implements SystemCityService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private SystemCityMapper systemCityMapper;

    @Resource
    private SystemCityAsyncService systemCityAsyncService;

    /**
     * 列表
     *
     * @param request 请求参数
     * @return List<SystemCity>
     */
    @Override
    public Object getList(SystemCitySearchRequest request) {
        Object list = redisUtil.hmGet(Constants.CITY_LIST, request.getParentId().toString());
        if (ObjectUtil.isNull(list)) {
            //城市数据，异步同步到redis，第一次拿不到数据，去数据库读取
            list = getList(request.getParentId());
        }
        return list;
    }

    /**
     * 修改城市
     *
     * @param id      城市id
     * @param request 修改参数
     */
    @Override
    public Boolean update(Integer id, SystemCityRequest request) {
        SystemCity systemCity = new SystemCity();
        BeanUtils.copyProperties(request, systemCity);
        systemCity.setId(id);
        boolean result = updateById(systemCity);
        if (result) {
            asyncRedis(request.getParentId());
        }
        return result;
    }

    /**
     * 修改状态
     *
     * @param id     城市id
     * @param status 状态
     */
    @Override
    public Boolean updateStatus(Integer id, Boolean status) {
        SystemCity systemCity = getById(id);
        systemCity.setId(id);
        systemCity.setIsShow(status);
        boolean result = updateById(systemCity);
        if (result) {
            asyncRedis(systemCity.getParentId());
        }
        return result;
    }

    @Override
    public List<SystemCityTreeVo> getListTree() {
        List<SystemCityTreeVo> cityList = redisUtil.get(Constants.CITY_LIST_TREE);
        if (CollUtil.isEmpty(cityList)) {
            systemCityAsyncService.setListTree();
        }
        return redisUtil.get(Constants.CITY_LIST_TREE);
    }

    /**
     * 根据父级id获取数据
     *
     * @param parentId integer parentId
     * @return Object
     * @author hanson.huang
     * @since 2025-01-23
     */
    public Object getList(Integer parentId) {
        LambdaQueryWrapper<SystemCity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemCity::getParentId, parentId);
        queryWrapper.in(SystemCity::getIsShow, true);
        return systemCityMapper.selectList(queryWrapper);
    }

    /**
     * 数据整体刷入redis
     *
     * @author hanson.huang
     * @since 2025-01-23
     */
    public void asyncRedis(Integer parentId) {
        systemCityAsyncService.async(parentId);
    }


}